/* ======================================  表单搜索、表格展示数据、分页  ====================================== */
const _ = {
	fetch(api, option={}){
		let opt = {
			paging:true,
			param:{},
			...option
		}
		const { last, isIndex, paging, param, reset, totalApi, COVID } = opt
	
		if(reset){
			this.searchFormRef?.reset?.()
			return _.onReset.call(this)
		}
		const _config = { param:{ ...this.query, ...this.model, ...param, ...last }, callback:option.callback, isIndex, loading:false, totalApi, COVID }
		if(totalApi){
			const { current, pageSize } = _config.param || {}
			const param = {
				page  	: current || 1, 		// 当前页
				limit 	: pageSize || window.$config.pageSize,		// 每页显示多少条数据
			    ..._config.param,
			}
			delete param.pageSize
			delete param.total
			delete param.current
			window.$http.pull(null, totalApi, {param}).then(data=>{
				this.setState({
					pag:{
						...this.state.pag,
						current		: +data.current, 		// 当前页码
						total		: +data.total_items,	// 总共多少条数据
						totalPage	: +data.total_pages,	// 总共多少页
						pageSize	: +data.limit,			// 每页显示多少条数据
					}
				})
			})
		}
		if(paging === true){
			return window.$http.paging(this,api, _config ).then(data=>{
				this.props?.onKeyChange?.()
				return data
			})
		}else if(paging === 'get'){
			delete _config.param.current
			return window.$http.pull(this,api, {..._config, pullLoading:'pagingLoading'} ).then(data=>{
				this.props?.onKeyChange?.()
				return data
			})
		}else if(paging === 'post'){
			return window.$http.submit(this,api, {..._config, submitLoading:'pagingLoading'} ).then(data=>{
				this.props?.onKeyChange?.()
				data = window.$fn.isArray(data) ? data : data.content || [] 
				if(isIndex){
					data.forEach((v,i)=> v.index = i )
				}
				this.setState({ data })
				return data
			})
		}
	},
	onChange({ model }) { 
		// this.model = {...this.model, ...model}
	},
	onSubmit(){
		// window.$fn.getBody(this.model)
		this.model = {...this.model, current:1 }
		_.clearKeys.call(this)
		this.fetch()
	},
	onSort(v){
		if(v){
			this.model = {...this.model, ...v}
		}else{
			delete this.model.sort
			delete this.model.sort_type
		}
		this.fetch()
	},
	onReset(){
		// if(window.$fn.hasArray(searchForm) && window.$fn.hasObject(this.model)){
		// 	searchForm.forEach(v=>{
		// 		const { name, names } = v
		// 		if(names){
		// 			delete this.model[names[0]]
		// 			delete this.model[names[1]]
		// 		}else{
		// 			delete this.model[name]
		// 		}
		// 	})
		// }
		
		const { pageSize } = this.model
		this.model = { pageSize, current:1 }
		
		if(this.resetModel){
			this.searchFormRef?.setValue?.(this.resetModel)
			this.model = JSON.parse(JSON.stringify(this.resetModel))
		}
		
		// 刷新某个部件
		let {  stateKey = 1 } = this.state
		stateKey ++ 
		this.setState({ stateKey })
		
		_.clearKeys.call(this)
		
		this.fetch()
	},
	pageChange({current, pageSize}){
		this.model = {...this.model, pageSize, current }
		this.fetch()
		if( this.clearKeys !== false ){ _.clearKeys.call(this) }
	},
	setKeys(selectedKeys,selectedRows){
		if(this.state.selectedRows && this.state.selectedKeys){
			this.setState({ selectedRows, selectedKeys }, ()=> this.props?.onKeyChange?.() )
		}else if(this.state.selectedKeys){
			this.setState({ selectedKeys }, ()=> this.props?.onKeyChange?.() )
		}
	},
	clearKeys(){
		if(this.state.selectedKeys && this.state.selectedRows){
			this.setState({ selectedKeys:this.selectedKeys||[], selectedRows:this.selectedRows||[]}, ()=> this.props?.onKeyChange?.())
		}else if(this.state.selectedKeys){
			this.setState({ selectedKeys:this.selectedKeys||[] }, ()=> this.props?.onKeyChange?.())
		}
	}
}

export default _